<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/2
 * Time: 上午9:06
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class GuestbookController extends AdminBaseController{

    //用于读取数据表的记录
    public function index(){
        $guest = D('guestbook')->guestList('id,uid,content,addtime,status','id desc'); //用于列表展示
        $this -> assign('list',$guest);
        $this -> display();
    }
    //第三种异步分页
    public function pageList(){
        sleep(1);
        $iteam = 3; //每页显示的条数
        $page = I('get.page/d',1);//当前页
        $title = I('get.title/s','');//获取的搜索内容的值
        $admin = M('Guestbook');
        $whereSql = empty($title)?'':'title like "%'.$title.'%"';
        $count = $admin->where($whereSql)->count(); //总的记录条数
        $pagenum = ceil($count/$iteam); //总页数
        $startPage = ($page - 1)*$iteam; //limit 的第一个参数，起始值。
        $list = $admin->where($whereSql)->order('id desc')->limit($startPage.','.$iteam) -> select(); //符合条件的记录
        $listPage = '';
        foreach($list as $key => $value) {
            $listPage .= '<volist name="list" id="va"><tr><td>' . $value['id'] . '</td><td>' . $value['gustname'] . '</td><td>'.mb_substr(strip_tags(htmlspecialchars_decode($value['content'])),0,10,'utf8').'</td><td>'.$value['addtime'].'</td>';
            if ($value['status']) {
                $listPage .= '<td>' . '<a href="javascript:changeStatus(' . $value['id'] . ')" id="status' . $value['id'] . '" class="label label-primary">已审核</a></td>';
            } else {
                $listPage .= '<td>' . '<a href="javascript:changeStatus(' . $value['id'] . ')" id="status' . $value['id'] . '" class="label label-danger">未审核</a></td>';
            }
            $listPage .= '<td>' . '<a href="' . U("Guestbook/edit?id=" . $value['id']) . '" class="btn btn-outline btn-primary"><i class="fa fa-edit"></i> 修改</a>
            <a href="javascript:del(' .$value['id']. ')" class="btn btn-outline btn-danger"><i class="fa fa-trash"></i> 删除</a>' . '</td></tr></volist>';
        }
        $data = [
            'pages' => $pagenum, //总页数
            'content' => $listPage
        ];
        exit(json_encode($data));
    }

    public function edit(){
            $id = I('get.id/d',0);
            $list = M("Guestbook")->field('id,content')->where(['id'=>$id])->find();
                if(!$list){
                    $this -> ajaxReturn(['status'=>1,'msg'=>'操作异常！']);
                }
            $this -> assign('list',$list);
            $this -> display();
    }

    public function save(){
            if(IS_POST || IS_AJAX){
                $id = I('post.id/d',0);
                $content = I('post.content/s','');
                $save = M("Guestbook")->where(['id'=>$id])->setField(['content'=>$content]);
                if(!$save){
                    $this -> ajaxReturn(['status'=>1,'msg'=>'修改失败！']);
                }
                $this -> ajaxReturn(['status'=>0,'msg'=>'修改成功！']);
            }else{
                $this -> ajaxReturn(['status'=>1,'msg'=>'非法操作！']);
            }
    }

    //修改审核状态
    public function changeStatus(){
        if(IS_POST){
            $status = D('guestbook')->changeStatus(I('post.id/d',0));
            $this -> ajaxReturn($status);
        }else{
            $this -> ajaxReturn(['status' => 1,'修改失败！']);
        }
    }

    //删除留言
    public function del(){
        if(IS_POST){
            $del = D('guestbook')->delguest(I('post.id/d',0));
            $this -> ajaxReturn(['status' => 0,'msg'=>'删除成功！']);
        }else{
            $this -> ajaxReturn(['status'=>1,'msg'=>'请重试！']);
        }
    }

}
